<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper高可用架构与优化指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #3a7bd5 0%, #00d2ff 100%);
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight-text {
            background: linear-gradient(90deg, #3a7bd5, #00d2ff);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        .mermaid-tooltip {
            position: absolute;
            background: white;
            padding: 8px;
            border-radius: 4px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            z-index: 100;
            display: none;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0 md:pr-10">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4">Zookeeper <span class="highlight-text">高可用架构</span>解密</h1>
                    <p class="text-xl md:text-2xl mb-6 opacity-90">探索分布式协调服务的稳定性和可靠性保障机制</p>
                    <div class="flex space-x-4">
                        <div class="flex items-center">
                            <i class="fas fa-shield-alt mr-2"></i>
                            <span>集群容错</span>
                        </div>
                        <div class="flex items-center">
                            <i class="fas fa-sync-alt mr-2"></i>
                            <span>数据复制</span>
                        </div>
                        <div class="flex items-center">
                            <i class="fas fa-bolt mr-2"></i>
                            <span>快速恢复</span>
                        </div>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-20 rounded-xl p-6 backdrop-filter backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                                A[Zookeeper集群] --> B[领导者选举]
                                A --> C[数据复制]
                                A --> D[故障恢复]
                                B --> E[ZAB协议]
                                C --> F[全量数据同步]
                                D --> G[自动failover]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-5xl">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <p class="text-lg leading-relaxed text-gray-700">
                    Zookeeper 的高可用性通过其集群架构、领导者选举机制和数据复制机制来实现。通过合理配置集群节点数量、超时时间、持久化设置、网络配置以及备份策略，可以进一步提高 Zookeeper 集群的可用性和稳定性。对这些配置进行精细化调整，结合实际应用需求，能够确保 Zookeeper 在高负载和节点故障的情况下，仍能保持良好的服务水平。
                </p>
            </div>
        </section>

        <!-- Cluster Architecture Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 bg-blue-500 rounded-full flex items-center justify-center text-white mr-4">
                    <i class="fas fa-network-wired text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">Zookeeper 的集群架构</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center text-blue-600 mr-3">
                            <i class="fas fa-crown"></i>
                        </div>
                        <h3 class="text-xl font-bold">领导者选举</h3>
                    </div>
                    <p class="text-gray-700">
                        Zookeeper 使用一种称为 ZAB（Zookeeper Atomic Broadcast）协议来保证数据的复制和一致性。领导者节点负责处理所有的写请求并将修改同步到所有的跟随者节点。通过选举机制，Zookeeper 能够在领导者节点失败时迅速选举出新的领导者，从而保证系统的高可用性。
                    </p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center text-green-600 mr-3">
                            <i class="fas fa-copy"></i>
                        </div>
                        <h3 class="text-xl font-bold">数据复制</h3>
                    </div>
                    <p class="text-gray-700">
                        所有的 Zookeeper 节点都持有一份完整的数据副本，领导者节点将数据变更同步到所有的跟随者节点。这样，即使某个节点出现故障，其他节点仍然可以继续提供服务。
                    </p>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="mermaid" style="min-height: 300px;">
                    flowchart LR
                        Client1[客户端] --> Leader[领导者]
                        Client2[客户端] --> Follower1[跟随者1]
                        Client3[客户端] --> Follower2[跟随者2]
                        Leader -- 数据同步 --> Follower1
                        Leader -- 数据同步 --> Follower2
                        Follower1 -- 心跳检测 --> Leader
                        Follower2 -- 心跳检测 --> Leader
                </div>
            </div>
        </section>

        <!-- Configuration Optimization Section -->
        <section>
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 bg-purple-500 rounded-full flex items-center justify-center text-white mr-4">
                    <i class="fas fa-sliders-h text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">配置优化以提高可用性</h2>
            </div>
            
            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                <!-- Node Configuration -->
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover group">
                    <div class="w-14 h-14 bg-purple-100 rounded-xl flex items-center justify-center text-purple-600 mb-4 group-hover:bg-purple-600 group-hover:text-white transition-colors">
                        <i class="fas fa-server text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">节点配置</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>奇数数量：</strong>通常配置3、5或7个节点以确保决策多数</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>最少节点数：</strong>至少需要3个节点以维持故障时的正常运作</span>
                        </li>
                    </ul>
                </div>
                
                <!-- Timeout Configuration -->
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover group">
                    <div class="w-14 h-14 bg-blue-100 rounded-xl flex items-center justify-center text-blue-600 mb-4 group-hover:bg-blue-600 group-hover:text-white transition-colors">
                        <i class="fas fa-clock text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">超时配置</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>会话超时：</strong>合理配置sessionTimeout以平衡故障检测和误判风险</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>连接超时：</strong>确保客户端能在合理时间内连接集群</span>
                        </li>
                    </ul>
                </div>
                
                <!-- Persistence Configuration -->
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover group">
                    <div class="w-14 h-14 bg-green-100 rounded-xl flex items-center justify-center text-green-600 mb-4 group-hover:bg-green-600 group-hover:text-white transition-colors">
                        <i class="fas fa-database text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">持久化配置</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>数据目录：</strong>配置dataDir指定数据存储目录</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>事务日志：</strong>配置dataLogDir存储事务日志以便恢复</span>
                        </li>
                    </ul>
                </div>
                
                <!-- Snapshot Configuration -->
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover group">
                    <div class="w-14 h-14 bg-yellow-100 rounded-xl flex items-center justify-center text-yellow-600 mb-4 group-hover:bg-yellow-600 group-hover:text-white transition-colors">
                        <i class="fas fa-camera text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">快照配置</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>快照频率：</strong>通过snapCount控制生成频率以减少系统负载</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>恢复效率：</strong>快照作为数据完整副本可加速恢复过程</span>
                        </li>
                    </ul>
                </div>
                
                <!-- Network Configuration -->
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover group">
                    <div class="w-14 h-14 bg-red-100 rounded-xl flex items-center justify-center text-red-600 mb-4 group-hover:bg-red-600 group-hover:text-white transition-colors">
                        <i class="fas fa-network-wired text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">网络配置</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>带宽要求：</strong>确保节点间有足够带宽和低延迟连接</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>网络稳定性：</strong>避免网络分区对集群的影响</span>
                        </li>
                    </ul>
                </div>
                
                <!-- Backup Strategy -->
                <div class="bg-white rounded-xl shadow-md p-6 transition-all duration-300 card-hover group">
                    <div class="w-14 h-14 bg-indigo-100 rounded-xl flex items-center justify-center text-indigo-600 mb-4 group-hover:bg-indigo-600 group-hover:text-white transition-colors">
                        <i class="fas fa-file-archive text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">备份策略</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>定期备份：</strong>定期备份数据以防灾难性故障</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>恢复测试：</strong>定期测试恢复过程确保备份有效性</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Best Practices Section -->
        <section class="mt-16">
            <div class="bg-gradient-to-r from-blue-50 to-indigo-50 rounded-xl p-8">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-indigo-500 rounded-full flex items-center justify-center text-white mr-4">
                        <i class="fas fa-lightbulb text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold">最佳实践总结</h2>
                </div>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white bg-opacity-80 rounded-lg p-5">
                        <div class="text-indigo-500 mb-3">
                            <i class="fas fa-check-double text-2xl"></i>
                        </div>
                        <h3 class="font-bold text-lg mb-2">集群规模</h3>
                        <p class="text-gray-700">3-7个节点的奇数规模，确保多数决策能力</p>
                    </div>
                    
                    <div class="bg-white bg-opacity-80 rounded-lg p-5">
                        <div class="text-indigo-500 mb-3">
                            <i class="fas fa-shield-alt text-2xl"></i>
                        </div>
                        <h3 class="font-bold text-lg mb-2">冗余设计</h3>
                        <p class="text-gray-700">全量数据复制，确保单点故障不影响服务</p>
                    </div>
                    
                    <div class="bg-white bg-opacity-80 rounded-lg p-5">
                        <div class="text-indigo-500 mb-3">
                            <i class="fas fa-tachometer-alt text-2xl"></i>
                        </div>
                        <h3 class="font-bold text-lg mb-2">性能监控</h3>
                        <p class="text-gray-700">实时监控关键指标，及时调整配置参数</p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto px-6 max-w-5xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索技术之美，分享架构之道</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-white transition-colors">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            securityLevel: 'loose'
        });
        
        // 添加简单的交互效果
        document.addEventListener('DOMContentLoaded', function() {
            // 卡片悬停效果
            const cards = document.querySelectorAll('.card-hover');
            cards.forEach(card => {
                card.addEventListener('mouseenter', function() {
                    this.style.transform = 'translateY(-5px)';
                });
                card.addEventListener('mouseleave', function() {
                    this.style.transform = '';
                });
            });
            
            // Mermaid图表交互
            const mermaidElements = document.querySelectorAll('.mermaid');
            mermaidElements.forEach(element => {
                element.addEventListener('click', function(e) {
                    if (e.target.tagName === 'rect' || e.target.tagName === 'path') {
                        const tooltip = document.createElement('div');
                        tooltip.className = 'mermaid-tooltip';
                        tooltip.textContent = e.target.getAttribute('title') || '点击查看更多信息';
                        tooltip.style.left = `${e.pageX + 10}px`;
                        tooltip.style.top = `${e.pageY + 10}px`;
                        document.body.appendChild(tooltip);
                        tooltip.style.display = 'block';
                        
                        setTimeout(() => {
                            tooltip.style.display = 'none';
                            tooltip.remove();
                        }, 2000);
                    }
                });
            });
        });
    </script>
</body>
</html>